package we.socialcontroller.zingme;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import we.common.CCommonHandler;
import we.socialcontroller.CSocialControllerException;


public class ZM_Friends extends ZM_API_Common {
    public ZM_Friends(ZM_Config config) {
        super(config, "/friends");
    }

    private static List<Long> parseRespone(String respone) throws CSocialControllerException {
        try {
            //Debug.trace(respone);
            JSONObject json = new JSONObject(respone);
            ArrayList<Long> result = new ArrayList();
            JSONArray arrJson = json.getJSONArray("uid");
            ArrayList arrFriend = arrJson.getArrayList();

            for (int i = 0; i < arrFriend.size(); i++) {
                result.add(Long.parseLong((arrFriend.get(i).toString())));
                //System.err.println(result.get(i));
            }
            return result;

        } catch (JSONException e) {
            CCommonHandler.writeErrLog(e);
            throw new CSocialControllerException(-3, "loi JSON");
        }        
    }


    public List<Long> getFriendList(String sessionId) throws CSocialControllerException {
        List<Long> result = new ArrayList();
        HashMap<String, String> args = new HashMap();
        args.put("session_key", sessionId);
        String respone = callMethod("Friends.getLists", args);
        return parseRespone(respone);
    }
}
